<?php
class ArticleModel extends Model
{
	/**
	 * 获取单页列表
	 */
	public static function get_page_list()
	{
		global $language_id;
		$cacheId = 'article_page_list_'.$language_id;
		if(S($cacheId)){
			$pageList = S($cacheId);
		}else{
			$pageList = array();
			$page_db = D('page');
			$where = array(
				'display' => 1,
				'language_id' => $language_id,
			);
			$page_db->join('page_items ON page_items.page_id = page.id');
			$pageArray = $page_db->where($where)->select();
			if(!$pageArray){
				$where = array(
					'display' => 1,
					'language_id' => C('language_id'),
				);
				$page_db->join('page_items ON page_items.page_id = page.id');
				$pageArray = $page_db->where($where)->select();
			}
			if($pageArray){
				foreach ($pageArray as $page){
					if(empty($page['name'])){
						$page = D('page_items')->where(array('page_id' => $page['id'], 'language_id' => C('language_id')))->find();
					}
					$page['url'] = url('article_help', 'id='.$page['id']);
					$pageList[$page['id']] = $page;
				}
				
				S($cacheId, $pageList);
			}
		}
		return $pageList;
	}
	/**
	 * 获取单页信息
	 */
	public static function get_page_info($id)
	{
		$pageList = self::get_page_list();
		return $pageList[$id] ? $pageList[$id] : false;
	}
	
/**
	 * 获取推荐文章列表
	 * @param int $cat_id 分类ID
	 * @param int $num	数量
	 */
	public static function get_recommend_article_list($cat_id, $num = 20)
	{
		global $language_id;
		$categoryInfo = CategoryModel::get_category_info($cat_id);
		if(!$categoryInfo){
			return false;
		}
		$articleList = array();
		$article_db = D('article');
		$article_db->field('id');
		$article_db->join('category_article AS ca ON ca.aid=article.id');
		$article_db->where('ca.catid='.$cat_id.' AND display = 1');
		$article_db->order('listorder DESC, `visit` DESC');
		$articleList = $article_db->limit($num)->select();
		if($articleList){
			foreach ($articleList as $key => &$article){
				$article = self::get_article_info($article['id']);
			}
		}
		return $articleList;
	}
	
	/**
	 * 获取文章总数
	 * @param array $post 筛选条件
	 * @return int
	 */
	public static function get_article_count($post)
	{
		global $language_id;
		
		$cat_id = isset($post['catid']) ? intval($post['catid']) : null;
		if($cat_id){
			$categoryInfo = CategoryModel::get_category_info($cat_id, CategoryModel::TYPE_ARTICLE);
			if(!$categoryInfo){
				$cat_id = null;
			}
			if($cat_id){
				$catids = CategoryModel::get_category_child_ids($cat_id, CategoryModel::TYPE_ARTICLE);
				$where['catid'] = array('in', $catids);
			}
		}
		
		$where['display'] = 1;
		$where['language_id'] = $language_id;
		if(isset($post['name']) && trim($post['name']) !== ''){
			$where['ai.name'] = array('LINK','%' . trim($post['name']) . '%');
		}
		
		$articleList = array();
		$article_db = D('article');
		$article_db->join('article_items AS ai ON ai.article_id = article.id');
		$article_db->where($where);
		$count = $article_db->count();
		return $count;
	}
	
	/**
	 * 获取文章列表
	 * @param array $post 筛选条件
	 * @param int $page
	 * @param int $pageRows
	 */
	public static function get_article_list($post = array(), $page = 1, $pageRows = 0)
	{
		global $language_id;
		
		$lang_id = $language_id;
		if(0==self::get_article_count($post)){
			$lang_id = C('language_id');
		}
		
		$cat_id = isset($post['catid']) ? intval($post['catid']) : null;
		if($cat_id){
			$categoryInfo = CategoryModel::get_category_info($cat_id, CategoryModel::TYPE_ARTICLE);
			if(!$categoryInfo){
				$cat_id = null;
			}
			if($cat_id){
				$catids = CategoryModel::get_category_child_ids($cat_id, CategoryModel::TYPE_ARTICLE);
				$where['catid'] = array('in', $catids);
			}
		}
		
		$where['display'] = 1;
		$where['language_id'] = C('language_id');
		if(isset($post['name']) && trim($post['name']) !== ''){
			$where['ai.name'] = array('LINK','%' . trim($post['name']) . '%');
		}
		$articleList = array();
		$article_db = D('article');
		$article_db->field('id');
		$article_db->join('article_items AS ai ON ai.article_id = article.id');
		$article_db->where($where);
		//排序
		if (isset($post['orderby']) && $post['orderby']) {
		  $article_db->order($post['orderby']);
		} else {
		  $article_db->order('article.listorder DESC, article.id ASC');
		}

		if ($pageRows) {
			$article_db->limit($page, $pageRows);
    	}
		$articleList = $article_db->select();
		if($articleList){
			foreach ($articleList as &$article){
				$article = self::get_article_info($article['id']);
			}
		}
		unset($article);
		return $articleList;
	}
	
	/**
	 * 获取文章详细内容
	 * @param int $aid 文章ID
	 */
	public static function get_article_info($aid)
	{
		global $language_id;
		$cacheId = 'article_'.$aid.'_'.$language_id;
		/*if(S($cacheId)){
			$info = S($cacheId);
		}else{*/
			$where['display'] = 1;
			$where['id'] = $aid;
			$where['language_id'] = $language_id;
			$articleList = array();
			$article_db = D('article');
			$article_db->join('article_items AS ai ON ai.article_id = article.id');
			$article_db->where($where);
			$info = $article_db->find();
			if(!$info){
				$where['language_id'] = C('language_id');
				$article_db = D('article');
				$article_db->join('article_items AS ai ON ai.article_id = article.id');
				$article_db->where($where);
				$info = $article_db->find();
			}
			if($info){
				$info['url'] = url('article_info', array('id' =>$info['id']));
			}
			/*S($cacheId, $info);
		}*/
		return $info;
	}
	public static function Surveyinsert($post){
		$survey_db = D('survey');
		$data = array(
		'question1' => $post['radioGroup1'],
		'question2' => $post['radioGroup2'],
		'question3' => $post['radioGroup3'],
		'question4' => $post['radioGroup4'],
		'question5' => $post['radioGroup5'],
		'question6' => $post['radioGroup6'],
		'question7' => $post['radioGroup7'],
		'message' =>$post['message'],
		'time' => time(),
		);	

		$survey_info =$survey_db->add($data); 
		return $survey_info;
	}
}